<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/2 0002
 * Time: 下午 13:31
 */
namespace  app\admin\controller;

use app\admin\controller\Permissions;
use \think\Db;
use \think\Session;
class  Recharge extends Permissions
{
    public function acount(){

        $agent=Db::name('ratio')->where('pid','>',0)->select();
        $daybegin=strtotime(date("Ymd"));
        $dayend=$daybegin+86400;
        $where['finish_time'] = [['>=',$daybegin],['<=',$dayend]];

        $count = array();
        foreach ($agent as $key => $value) {
            $agent_id = $value['agent_id'];
            $count[$key]['recharge_money'] = Db::name('order')->where('agent_id',$agent_id)->where($where)->sum('recharge_money');
            $count[$key]['time'] = date('Y-m-d H:i:s');
            $count[$key]['agent_id'] = $value['agent_id'];
            $count[$key]['phone'] = Db::name('agent')->where('id',$agent_id)->value('phone');
        }

        foreach ($count as $key=>$value){
            $id[$key] = $value['recharge_money'];
            $price[$key] = $value['time'];
        }
        if($count){
        array_multisort($id,SORT_NUMERIC,SORT_DESC,$price,SORT_STRING,SORT_ASC,$count);
        $count = array_slice($count, 0,10);
        }
      
        $this->assign('order', $count);
        return $this->fetch();
    }

    public function consume()
    {
        //根据所有代理商连接游戏玩家
        $agent = Db::name('ratio')->alias('ra')->join('relation re', 'ra.request=re.invitation_code' , 'left')
            ->join('agent a' , 'a.id=ra.agent_id', 'left')
            ->where('ra.pid != 0')->field('a.phone,ra.agent_id,ra.request,GROUP_CONCAT(re.game_user_id) as game_user_id')->group('ra.agent_id')->select();
        if($agent){
            $today = date('Y-m-d 00:00:00' , time());
            foreach($agent as $k=>$v){
                $Score[$v['agent_id']]['phone']         =   $v['phone'];
                $Score[$v['agent_id']]['agent_id']      =   $v['agent_id'];
                $Score[$v['agent_id']]['time']          =   date('Y-m-d H :i:s');
                //单个代理商房卡消耗量
                /*$Single[$v['agent_id']]['gameuser'][]    =   $v['game_user_id'] ? $v['game_user_id'] : 0;
                $Single[$v['agent_id']]['agent_id']      =   $v['agent_id'];*/
                $gameuser   =   $v['game_user_id'] ? $v['game_user_id'] : '';
                $Score[$v['agent_id']]['sum']   =   getSqlConnec("QPTreasureDB_CHUI" , 'RecordFangKaCost')->where("CostTime >= '{$today}'")->where("UserID in ('{$gameuser}')")->sum('CostCount');
            }
            foreach ($Score as $key=>$value){
                $id[$key] = $value['sum'];
            }
            array_multisort($id,SORT_NUMERIC,SORT_DESC,$Score);
            $Score = array_slice($Score, 0,10);

        }else{
            $Score = array();
        }

        
        $this->assign('Score', $Score);
        return $this->fetch();
    }
}